package com.cy.service;


import com.cy.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Random;

@Service
public class MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private UserDao userDao;

    @Value("1972551648@qq.com")
    private String from;

    public void sendMimeMail(String email, HttpSession session) {
        SimpleMailMessage message = new SimpleMailMessage();
        //配置邮箱主题
        message.setSubject("Tedu - 验证码邮件");
        //生成六位数随机数
        String code = randomCode();
        //将随机数保存到session中
        session.setAttribute("smail", email);
        session.setAttribute("code", code);

        message.setText("您收到的验证码是:"+code+".请您在五分钟发送");
        //设置收件人
        message.setTo(email);
        //设置寄件人
        message.setFrom(from);
        //发送邮件
        javaMailSender.send(message);
    }
    private String randomCode(){
        StringBuilder sb =new StringBuilder();
        Random random = new Random();
        for (int i = 0; i <6; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }
}
